package com.estar.integaration.Controller.MoneyController;

import com.estar.integaration.Util.Util;
import com.estar.integaration.entity.NoSQL.Res;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Vector;

/**
 * Author: YJL
 * Date: 2022/1/21 10:44
 **/

@RestController
@RequestMapping("/production-shop")
public class ProductionShop {

  @Value("${spring.profiles.active}")
  private String mEnvironment;

  @PostMapping("/import-production-shop")
  @ResponseBody
  public Res importExcel(HttpServletRequest request) {
    String folderAddress = mEnvironment.equals("dev") ? "D:\\SCM_file\\inventoryFile\\productionShop" : "/root/excel/inventoryFile/productionShop";
//    String folderAddress =  "/root/excel/inventoryFile/productionShop";
    //删除原文件夹全部文件
    Util.deleteAllFile(new File(folderAddress));
    //添加进去新文件
    MultipartHttpServletRequest params = (MultipartHttpServletRequest) request;
    List<MultipartFile> multipartFiles = params.getFiles("files");
    Vector<String> multipartFilePath = new Vector<>();//上传文件的地址
    for (MultipartFile file : multipartFiles) {
      //文件写入本地
      String address = Util.writeInventoryFile2Local(file,mEnvironment,"productionShop");
      multipartFilePath.add(address);
    }
    return new Res("20000","succ");//上传成功
  }



}
